package com.zhiche.lisa.bms.service.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.zhiche.lisa.bms.pojo.dto.util.mdm.ServiceTypeDTO;
import com.zhiche.lisa.bms.pojo.dto.util.mdm.TransportationTypeDTO;
import com.zhiche.lisa.bms.pojo.vo.fee.FleetTypeVO;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.core.utils.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * Created by zhaoguixin on 2018/6/29.
 */
@Component
public class MdmUtil {
	@Value("${lisa.mdm.url}")
	private String url;

	/**
	 * 连接超时时间
	 */
	@Value("${lisa.socketTimeout}")
	private int socketTimeout;

	@Value("${lisa.mdm.getAllFleetTypeByTypeUr1}")
	private String getAllFleetTypeByTypeUr1;

	@Value("${lisa.mdm.getTransportTypeById}")
	private String getTransportTypeById;

	@Value("${lisa.mdm.getTransportTypeByServiceTypeId}")
	private String getTransportTypeByServiceTypeId;

	// @Value("${lisa.mdm.queryTransModelUr1}")
	// private String queryTransModelUr1;

	@Value("${lisa.mdm.getAllServiceTypeList}")
	private String getAllServiceTypeUrl;

	/**
	 * 根据车队类型Id获取车队类型
	 *
	 * @return
	 */
	public List<FleetTypeVO> getAllFleetTypeByType() {
		List<FleetTypeVO> listFleetType = Lists.newArrayList();
		List<NameValuePair> headNamePairs = Lists.newArrayList();
		headNamePairs.add(new BasicNameValuePair("Authorization", ""));
		List<NameValuePair> params = Lists.newArrayList();
//    params.add(new BasicNameValuePair("fleetTypeId",fleetTypeId));
		String strReturn = HttpClientUtil.get(url + getAllFleetTypeByTypeUr1, headNamePairs, params, socketTimeout);
		if (StringUtils.isNotEmpty(strReturn)) {
			RestfulResponse<List<FleetTypeVO>> restfulResponse =
					JSON.parseObject(strReturn, new TypeReference<RestfulResponse<List<FleetTypeVO>>>() {
					});
			if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
				listFleetType = restfulResponse.getData();
			}
		}
		return listFleetType;
	}

	/**
	 * 根据id获取运输类型
	 *
	 * @param id
	 * @return
	 */
	public TransportationTypeDTO getTransportTypeById(Long id) {
		TransportationTypeDTO transportationType = null;
		try {
			List<NameValuePair> headNamePairs = Lists.newArrayList();
			headNamePairs.add(new BasicNameValuePair("Authorization", ""));
			List<NameValuePair> params = Lists.newArrayList();
			params.add(new BasicNameValuePair("transportTypeId", String.valueOf(id)));
			String strReturn = HttpClientUtil.get(url + getTransportTypeById, headNamePairs, params, socketTimeout);
			if (StringUtils.isNotEmpty(strReturn)) {
				RestfulResponse<TransportationTypeDTO> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<TransportationTypeDTO>>() {
				});
				if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
					transportationType = restfulResponse.getData();
				}
			}
		} catch (Exception ex) {
			throw new BaseException(ex.getMessage());
		}
		return transportationType;
	}

	public List<TransportationTypeDTO> getTransportTypeByServiceTypeId(Long id) {
		List<TransportationTypeDTO> transportationType = null;
		try {
			List<NameValuePair> headNamePairs = Lists.newArrayList();
			headNamePairs.add(new BasicNameValuePair("Authorization", ""));
			List<NameValuePair> params = Lists.newArrayList();
			params.add(new BasicNameValuePair("serviceTypeId", String.valueOf(id)));
			String strReturn = HttpClientUtil.get(url + getTransportTypeByServiceTypeId, headNamePairs, params, socketTimeout);
			if (StringUtils.isNotEmpty(strReturn)) {
				RestfulResponse<List<TransportationTypeDTO>> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<List<TransportationTypeDTO>>>() {
				});
				if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
					transportationType = restfulResponse.getData();
				}
			}
		} catch (Exception ex) {
			throw new BaseException(ex.getMessage());
		}
		return transportationType;
	}

	/**
	 * 获取对应的运输方式
	 *
	 * @return
	 */
	public List<ServiceTypeDTO> getAllServiceTypeList() {
		List<ServiceTypeDTO> serviceTypeList = Lists.newArrayList();
		String strReturn = HttpClientUtil.get(url + getAllServiceTypeUrl, null, null, socketTimeout);
		if (StringUtils.isNotEmpty(strReturn)) {
			RestfulResponse<List<ServiceTypeDTO>> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<List<ServiceTypeDTO>>>() {
			});
			if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
				serviceTypeList = restfulResponse.getData();
			}
		}
		return serviceTypeList;
	}

//    /**
//     * 根据 ids 获取运输方式
//     *
//     * @return
//     */
//    public Map<Long,String> queryTransModel(Map<String, FeeReceivableDTO> map, String token) {
//        Map<Long,String> mapS = Maps.newHashMap();
//        List<NameValuePair> headNamePairs = Lists.newArrayList();
//        headNamePairs.add(new BasicNameValuePair("Authorization", token));
//        String parmsJson = JSON.toJSONString(map);
// //        List<NameValuePair> params = Lists.newArrayList();
// //    params.add(new BasicNameValuePair("fleetTypeId",fleetTypeId));
//        String strReturn = HttpClientUtil.postJson(queryTransModelUr1, headNamePairs, parmsJson, socketTimeout);
//        if (StringUtils.isNotEmpty(strReturn)) {
//            RestfulResponse<Map<Long,String>> restfulResponse =
//                    JSON.parseObject(strReturn, new TypeReference<RestfulResponse<Map<Long,String>>>() {
//                    });
//            if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
//                mapS = restfulResponse.getData();
//            }
//        }
//        return mapS;
//    }
}
